# 获取每日估值数据

import requests
import time
from datetime import datetime, date

from cachelib import SimpleCache

cache = SimpleCache()
# 12h
cache_timeout = 12 * 60 * 60

'''
int(time.time()) 
https://xueqiu.com/v4/statuses/user_timeline.json?page=1&user_id=3079173340&_=1616938903936

'''


def get_xueqiu():
    xueqiu_lsd = 'https://xueqiu.com/v4/statuses/user_timeline.json?page=1&user_id=3079173340&_='

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
        'Cookie': 'device_id=fc8ff5511d1b4303919f3f480bd090d7; s=df114m2kbv; snbim_minify=true; bid=de23368341b590b912f08d8bd4f302dd_kksdgn55; xq_a_token=ac5cb867e99a8f0523cd09a4ab3cd1cf6936b9be; xqat=ac5cb867e99a8f0523cd09a4ab3cd1cf6936b9be; xq_r_token=61b4ebfcc719c8ca1c4313affdcf71add750508e; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjQwMjIzMTU2NjksImlzcyI6InVjIiwiZXhwIjoxNjE3ODUwMTQyLCJjdG0iOjE2MTU5MzY3MTY0MzEsImNpZCI6ImQ5ZDBuNEFadXAifQ.Jcvi8H61cZ7jhu3m7cyodYaQzdjwuz7yeFXkx1JDXBwCTvMqUJjTxFxIGCnxjmI9DjQQaUJqZ2htogiPgNHRpJ_kMp3E_et1VXU8AzLrcPEjzgHvTpqe8RP4alxX4rZFx73Yt68E8E_Mp4Ys5Irtjmlpm-2aWQo6AJYDUgpxlMVeqZTgrKqpuBBqkTa1-BwUbfTpPqo98KvqoP_MldEjCEPC0ruHgA0wRWje46rbifFj7FIf0tVp7_JckKnwori7ssvREtlLj29G3dxXzqLKiq6GV-V4l_Az89LNpJ23AYJA_mFPA0MEcjmdh7sTbc_O3NLSn97QVIlLmq5q4ain3Q; xq_is_login=1; u=4022315669; Hm_lvt_1db88642e346389874251b5a1eded6e3=1616929133,1616929141,1616929414,1616930001; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1616939071; acw_tc=2760820416169408725174698eaf8cbd1d4097b4ed2c9be875582ad3cec78c'
    }

    r = requests.get(xueqiu_lsd + str(int(time.time())), headers=headers)

    print(r.text)


def get_qieman():
    data = cache.get('get-qieman')
    x_sign = get_qieman_sign()

    if data is None:
        r = requests.get(url='https://qieman.com/pmdj/v2/idx-eval/latest',
                         headers={'x-sign': x_sign,
                                  'Accept': 'application/json',
                                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'})

        if (r.status_code == 200):
            try:
                if r.text == '':
                    # 清除缓存, 下次请求时
                    cache.set('get-qieman-sign', None, timeout=cache_timeout)

                    print('s_sign 无效')

                data = r.json();
                ts = data['date']
                data['date'] = date.fromtimestamp(ts / 1000)

                cache.set('get-qieman', data, timeout=cache_timeout)
            except BaseException:
                data = {'date': '数据获取异常'}
        else:
            data = {'date': '获取失败'}
    return data


def get_qieman_sign():
    '''获取且慢 x-sign
    :return:
    '''
    sign = cache.get('get-qieman-sign')
    if sign is None:
        sign = ''
    return sign


def get_danjuan_index():
    '''蛋卷基金-指数估值

    :return:
    '''
    data = cache.get('get_danjuan_index')
    if data is None:
        r = requests.get('https://danjuanapp.com/djapi/index_eva/dj',
                         headers={'Accept': 'application/json',
                                  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'})
        if r.status_code == 200:
            try:
                data = r.json()
                items = data['data']['items']
                if len(items):
                    ts = date.fromtimestamp(items[0]['ts'] / 1000)
                    data['date'] = ts
                cache.set('get_danjuan_index', data, timeout=cache_timeout)
            except BaseException:
                data = {'date': '数据获取异常'}
        else:
            data = {'date': '获取失败'}
    return data
